package com.learn.shop.controller;

import com.learn.shop.constant.R;
import com.learn.shop.pojo.entriy.SUser;
import com.learn.shop.pojo.vo.UpdateUserVo;
import com.learn.shop.service.SUserService;
import com.learn.shop.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@Api(tags = "用户模块")
@RestController
@RequestMapping("user")
public class UserLoginController {

    @Autowired
    private SUserService sUserService;

    @ApiOperation("登录")
    @GetMapping("/login")
    public R login(@RequestParam("username") String username, @RequestParam("password") String password) {
        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
            SUser sUser = sUserService.queryByName(username);
            //判断用户名和密码是否正确
            if (username.equals(sUser.getUserName())) {
                boolean res = SecurityUtils.matchesPassword(password, sUser.getPassword());
                if (res) {
                    return R.ok("登录成功!");
                } else {
                    return R.fail("密码错误!");
                }
            } else {
                return R.fail("您输入的用户名不存在!");
            }
        } else {
            return R.fail("用户名或密码不能为空!");
        }
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public R register(@RequestBody SUser user) {
        if (user != null) {
            if (StringUtils.isNotBlank(user.getUserName()) && StringUtils.isNotBlank(user.getPassword())) {

                //判断用户名是否重复
                SUser res = sUserService.queryByName(user.getUserName());
                if (res != null) {
                    return R.fail("用户名已存在!");
                }
                try {
                    //生成主键id
                    user.setUserId(UUID.randomUUID().toString());
                    //对密码进行加密
                    user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
                    sUserService.insert(user);
                    return R.ok("注册成功!");
                } catch (Exception e) {
                    e.printStackTrace();
                    return R.fail("注册失败,请稍后再试!");
                }

            } else {
                return R.fail("用户名和密码不能为空!");
            }
        } else {
            return R.fail("用户名和密码不能为空!");
        }
    }

    @ApiOperation("修改密码")
    @PostMapping("/updatePassword")
    public R updatePassword(@RequestBody UpdateUserVo updateUserVo) {
        if (updateUserVo != null) {
            if (StringUtils.isNotBlank(updateUserVo.getUserName())
                    && StringUtils.isNotBlank(updateUserVo.getPassword())
                    && StringUtils.isNotBlank(updateUserVo.getNewPassword())) {
                SUser sUser = sUserService.queryByName(updateUserVo.getUserName());
                if (sUser != null) {
                    boolean res = SecurityUtils.matchesPassword(updateUserVo.getPassword(), sUser.getPassword());
                    if (res) {
                        if (updateUserVo.getPassword().equals(updateUserVo.getNewPassword())) {
                            return R.fail("新密码不能和旧密码相同!");
                        }
                    } else {
                        return R.fail("旧密码错误");
                    }
                    try {
                        sUser.setUserName(sUser.getUserName());
                        sUser.setPassword(SecurityUtils.encryptPassword(updateUserVo.getNewPassword()));
                        sUserService.update(sUser);
                        return R.ok("修改密码成功!");
                    } catch (Exception e) {
                        e.printStackTrace();
                        return R.fail("修改失败");
                    }
                } else {
                    return R.fail("用户名错误!");
                }
            } else {
                return R.fail("用户名和密码不能为空!");
            }
        } else {
            return R.fail("用户名和密码不能为空!");
        }
    }


}
